<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表格全选</title>
    <style>
        table{
            border: 1px solid;
            width: 500px;
            margin-left: 30%;
        }

        td,th{
            text-align: center;
            border: 1px solid;
        }
        div{
            margin-top: 10px;
            margin-left: 30%;
        }

        .over{
            background-color: pink;
        }
        .out{
            background-color: white;
        }
    </style>

    <script>
        /**
         * 分析
         * 获取所有checkbox
         * 遍历cb,设置每一个cb的状态为选中  check
         */

    //1.onload
        window.onload = function () {
            //2.全选按钮绑定单击事件
            document.getElementById("selectAll").onclick = function () {
                //给所有checkbox都加上name  获取所有的checkbox
                var cbs = document.getElementsByName("cb");
                for (var i = 0; i < cbs.length; i++) {
                    cbs[i].checked = true;
                }}
                document.getElementById("unSelectAll").onclick = function () {
                    //给所有checkbox都加上name  获取所有的checkbox
                    var cbs = document.getElementsByName("cb");
                    for (var i = 0; i < cbs.length; i++) {
                        cbs[i].checked = false;
                    }
                }
                document.getElementById("selectRev").onclick = function () {
                    //给所有checkbox都加上name  获取所有的checkbox
                    var cbs = document.getElementsByName("cb");
                    for (var i = 0; i < cbs.length; i++) {
                        //     if (cbs[i].checked = false){
                        //         cbs[i].checked = true;
                        //     }
                        //     else{cbs[i].checked = false
                        //     }
                        cbs[i].checked = !cbs[i].checked
                    }


                }
            document.getElementById("firstcb").onclick = function () {
                //给所有checkbox都加上name 获取所有的checkbox
                var cbs = document.getElementsByName("cb");
                //获取 第一个cb 本身就是获取的
                // alert(this)// 弹出显示的就是自己
                for (var i = 0; i < cbs.length; i++) {
                    cbs[i].checked=this.checked;
                }
            //把所有tr都搞出来
            var trs = document.getElementsByTagName("tr");
                for (var i = 0; i < trs.length; i++) {
                    trs[i].onmouseover=function () {
                        this.className="over";
                    }
                    trs[i].onmouseout=function () {
                        this.className="out";

                    }
                }
            }
        }
    </script>

</head>
<body>

<table>
    <caption>学生信息表</caption>
    <tr>
        <th><input type="checkbox" name="cb" id="firstcb"></th>
        <th>编号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>操作</th>
    </tr>

    <tr>
        <td><input type="checkbox" name="cb"></td>
        <td>1</td>
        <td>令狐冲</td>
        <td>男</td>
        <td><a href="javascript:void(0);">删除</a></td>
    </tr>

    <tr>
        <td><input type="checkbox" name="cb"></td>
        <td>2</td>
        <td>任我行</td>
        <td>男</td>
        <td><a href="javascript:void(0);">删除</a></td>
    </tr>

    <tr>
        <td><input type="checkbox" name="cb"></td>
        <td>3</td>
        <td>岳不群</td>
        <td>?</td>
        <td><a href="javascript:void(0);">删除</a></td>
    </tr>

</table>
<div>
    <input type="button" id="selectAll" value="全选">
    <input type="button" id="unSelectAll" value="全不选">
    <input type="button" id="selectRev" value="反选">
</div>
</body>
</html>